using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class UserCustomProperty : BaseItem
    {
        private UserCustomProperty() : base() { }
        private UserCustomProperty(Hashtable table) : base(table) { }
        private UserCustomProperty(Hashtable table, List<UserCustomProperty> collection) : base(table, collection) { }

        public static UserCustomProperty CreateInstance(int userId, string name, string value)
        {
            UserCustomProperty tmp = new UserCustomProperty();
            tmp.userId = userId;
            tmp.name = name;
            tmp.value = value;
            return tmp;
        }

        private static UserCustomProperty Get(Hashtable table)
        {
            if (table != null)
                return new UserCustomProperty(table);
            else
                return null;
        }

        private static List<UserCustomProperty> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<UserCustomProperty> tmp = new List<UserCustomProperty>();
                foreach(Hashtable table in tables)
                {
                    tmp.Add(new UserCustomProperty(table, tmp));
                }
                return tmp;
            }
            else
                return null;
        }

        public static List<UserCustomProperty> GetAllByUserId(int userId)
        {
            UserCustomPropertyDal dal = new UserCustomPropertyDal();
            return Get(dal.GetAllByUserId(userId));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((UserCustomPropertyDal)dal).Update(ref id, userId, name, value, ref concurencyId);
        }

        protected override Base CreateDal()
        {
            return new UserCustomPropertyDal();
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[UserCustomPropertyDal.Fields.Id];
            userId = (int)table[UserCustomPropertyDal.Fields.UserId];
            name = (string)table[UserCustomPropertyDal.Fields.Name];
            value = (string)table[UserCustomPropertyDal.Fields.Value];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int userId;
        public int UserId
        {
            get { return userId; }
        }

        private string name;
        public string Name
        {
            get { return name; }
        }

        private string value;
        public string Value
        {
            get { return value; }
            set { this.value = value; }
        }

        internal void SetUser(UserSecurity userSecurity)
        {
            this.userId = userSecurity.Id;
        }
    }
}
